package com.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.demo.model.ActiveData;
import com.demo.model.ActiveTotalData;
import com.demo.model.LianJia;
import com.demo.result.Result;
import com.demo.service.LianjiaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Tag(name = "房源查询接口")
public class HouseController {
    @Autowired
    private LianjiaService lianjiaService;

    @GetMapping ("/selectAllHouse")
    @ResponseBody
    @Operation(summary  = "房源查询列表",
            parameters = {
                    @Parameter(
                            name = "quyu",
                            description = "区域名称，如 '朝阳区'、'海淀区'。不传时查询所有区域",
                            required = false,
                            schema = @Schema(type = "string")
                    ),
                    @Parameter(name = "pageNum", description = "页码", required = true),
                    @Parameter(name = "pageSize", description = "每页记录数", required = true)
            })
            public Result searchByQuYu(
            @RequestParam(value = "quyu", required = false) String quyu,
            @RequestParam("pageNum") int pageNum,
            @RequestParam("pageSize") int pageSize) {

        // 调用修改后的Service方法获取分页对象
        IPage<LianJia> pageResult = lianjiaService.searchByQuYuNew(quyu, pageNum, pageSize);

        // 构建返回结果，包含总记录数和当前页数据
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("total", pageResult.getTotal());    // 总记录数
        resultMap.put("rows", pageResult.getRecords());  // 当前页数据列表

        return Result.success(resultMap);
    }
    /*
     *  获取数据头信息
     * */
    @GetMapping("/searchByJj")
    @Operation(summary  = "查询日、月、年分析数据",description = "查询日、月、年分析数据")
    @ResponseBody
    public Result searchByJj(String thisEntryDate) {
        List<ActiveTotalData> activeTotalData = lianjiaService.searchByJj(thisEntryDate);
        if (activeTotalData == null){
            return Result.error();
        }
        return Result.success(activeTotalData);
    }
    @GetMapping("/searchByData")
    @Operation(summary  = "查询日月年数量变化",description = "查询日月年数量变化")
    @ResponseBody
    public Result searchByData(String tag, String thisEntryDate) {
        List<ActiveData> activeData = lianjiaService.searchByData(tag, thisEntryDate);
        if (activeData == null){
            return Result.error();
        }
        return Result.success(activeData);
    }






}
